package itbour.onetouchshow.utils.gsonanalysis;

import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import itbour.onetouchshow.bean.inching.noppt.InchingCoreRootNoPPTV10Bean;
import itbour.onetouchshow.bean.inching.noppt.InchingRelationsV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootImageValueV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootShapeValueV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootTableValueV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootTextValueV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootVideoValueV10Bean;

/**
 * Created by zjl_d on 2017/11/21.
 */

public class CoreJsonToObjNoPPTRules implements JsonDeserializer<InchingCoreRootNoPPTV10Bean> {
    @Override
    public InchingCoreRootNoPPTV10Bean deserialize(JsonElement json, Type jsonType, JsonDeserializationContext context) throws JsonParseException {
        InchingCoreRootNoPPTV10Bean data = new InchingCoreRootNoPPTV10Bean();


        JsonObject jsonObject = json.getAsJsonObject();

        Long id = jsonObject.get("id").getAsLong();
        String topic = jsonObject.get("topic").getAsString();
        int kind = jsonObject.get("kind").getAsInt();
        int type = jsonObject.get("type").getAsInt();

        data.setId(id);
        data.setTopic(topic);
        data.setKind(kind);
        data.setType(type);


        JsonElement jsonElementChildMax = jsonObject.get("child_max");
        if (jsonElementChildMax == null || jsonElementChildMax.isJsonNull()) {
            data.setChild_max(1);
        } else {
            data.setChild_max(jsonElementChildMax.getAsInt());
        }

        JsonElement jsonElementH = jsonObject.get("h");
        if (jsonElementH == null || jsonElementH.isJsonNull()) {
            data.setH(0);
        } else {
            data.setH(jsonElementH.getAsInt());
        }

        JsonElement jsonElementO = jsonObject.get("o");
        if (jsonElementO == null || jsonElementO.isJsonNull()) {
            data.setO(0);
        } else {
            data.setO(jsonElementO.getAsInt());
        }


        JsonElement jsonElementP = jsonObject.get("p");
        if (jsonElementP == null || jsonElementP.isJsonNull()) {
            data.setP(0);
        } else {
            data.setP(jsonElementP.getAsInt());
        }

        JsonElement jsonElementCt = jsonObject.get("ct");
        if (jsonElementCt == null || jsonElementCt.isJsonNull()) {
            data.setCt(null);
        } else {
            int ct = jsonObject.get("ct").getAsInt();
            data.setCt(ct);
        }

        /**
         * 如果取不到 说明是1，2版本的
         */
        JsonElement jsonElementPageIndex = jsonObject.get("page_index");
        if (jsonElementPageIndex == null || jsonElementPageIndex.isJsonNull()) {
            data.setPage_index(null);
        } else {
            int page_index = jsonObject.get("page_index").getAsInt();
            data.setPage_index(page_index);
        }
        JsonElement jsonElementObjId = jsonObject.get("obj_id");
        if (jsonElementObjId == null || jsonElementObjId.isJsonNull()) {
            data.setObj_id(null);
        } else {
            int obj_id = jsonObject.get("obj_id").getAsInt();
            data.setObj_id(obj_id);
        }


        //value 的类型为文字
        if (type == InchingCoreRootNoPPTV10Bean.SJGNodeTypeText) {
            NoPPTRootTextValueV10Bean quickEditActRootValueText = context.deserialize(jsonObject.get("value"), NoPPTRootTextValueV10Bean.class);
            data.setValue(quickEditActRootValueText);
        }
        //value的类型为图片
        else if (type == InchingCoreRootNoPPTV10Bean.SJGNodeTypeImage) {
            JsonElement jsonElementValue = jsonObject.get("value");
            NoPPTRootImageValueV10Bean quickEditActRootValueImage = context.deserialize(jsonElementValue, NoPPTRootImageValueV10Bean.class);
            data.setValue(quickEditActRootValueImage);
        } else if (type == InchingCoreRootNoPPTV10Bean.SJGNodeTypeTable) {//4为表格类型
            JsonElement jsonElementValue = jsonObject.get("value");
            NoPPTRootTableValueV10Bean designOrPrintTableBeanV54 = context.deserialize(jsonElementValue, NoPPTRootTableValueV10Bean.class);
            data.setValue(designOrPrintTableBeanV54);
        } else if (type == InchingCoreRootNoPPTV10Bean.SJGTGNodeModeTypeShape) {//5为图形类型 v62 将其和文字区分开
            JsonElement jsonElementValue = jsonObject.get("value");
            NoPPTRootShapeValueV10Bean designOrPrintTableBeanV54 =
                    context.deserialize(jsonElementValue, NoPPTRootShapeValueV10Bean.class);
            data.setValue(designOrPrintTableBeanV54);
        } else if (type == InchingCoreRootNoPPTV10Bean.SJGNodeTypeVideo) {  //7为视频编辑
            JsonElement jsonElementValue = jsonObject.get("value");
            NoPPTRootVideoValueV10Bean designOrPrintTableBeanV54 =
                    context.deserialize(jsonElementValue, NoPPTRootVideoValueV10Bean.class);
            data.setValue(designOrPrintTableBeanV54);
        }

        JsonArray childrenList = jsonObject.get("children").getAsJsonArray();
        List<InchingCoreRootNoPPTV10Bean> quickEditActiviyRootList = new ArrayList<InchingCoreRootNoPPTV10Bean>();
        if (childrenList.size() != 0) {
            for (int i = 0; i < childrenList.size(); i++) {
                InchingCoreRootNoPPTV10Bean deserialize = context.deserialize(childrenList.get(i), InchingCoreRootNoPPTV10Bean.class);
                quickEditActiviyRootList.add(deserialize);
            }

            data.setChildren(quickEditActiviyRootList);
        }

        //加入新加入的字段relations
        JsonElement jsonRelations = jsonObject.get("relations");
        ArrayList<InchingRelationsV10Bean> relationList = new ArrayList<>();
        if (jsonRelations == null || jsonRelations.isJsonNull()) {

        } else {
            JsonArray asJsonArray = jsonRelations.getAsJsonArray();
            for (int i = 0; i < asJsonArray.size(); i++) {


                InchingRelationsV10Bean deserialize = context.deserialize(asJsonArray.get(i), InchingRelationsV10Bean.class);
                relationList.add(deserialize);
            }
            data.setRelations(relationList);
        }


        JsonElement jsonElementChildTemplate = jsonObject.get("child_template");
        //为Null就不添加这个字段
        if (jsonElementChildTemplate == null || jsonElementChildTemplate.isJsonNull()) {
        } else {
            InchingCoreRootNoPPTV10Bean child_template = context.deserialize(jsonElementChildTemplate, InchingCoreRootNoPPTV10Bean.class);
            data.setChild_template(child_template);
        }


        return data;
    }
}
